/**
 * \* Created with IntelliJ IDEA.
 * \* User: 冯若航
 * \* Date: 2021/7/1
 * \* Time: 18:36
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class 寻找目标值左侧边界 {
    int left_bound(int[] nums,int target){
        int left=0,right=nums.length-1;
        while(left<=right){
            int mid=left+(right-left)/2;
            if(nums[mid]<target){
                left=mid+1;
            }else if(nums[mid]>target){
                right=mid-1;
            }else if(nums[mid]==target){
                right=mid-1;
            }
        }
        //检查越界情况或者找不到目标值情况
        if(left>= nums.length||nums[left]!=target){
            return -1;
        }
        return left;
    }

    /**
     * 寻找右侧边界
     */
    int right_bound(int[] nums,int target){
        int left=0,right=nums.length-1;
        while(left<=right){
            int mid=left+(right-left)/2;
            if(nums[mid]<target){
                left=mid+1;
            }else if(nums[mid]>target){
                right=mid-1;
            }else if(nums[mid]==target){
                left=mid+1;
            }
        }
        //检查越界情况或者找不到目标值情况
        if(left<0||nums[left]!=target){
            return -1;
        }
        return left;
    }
}